package com.hong.qrpc.codec.impl;

import com.caucho.hessian.io.Hessian2Output;
import com.hong.qrpc.codec.Encoder;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @Author HQT
 * @Date 2022/8/4 9:31
 */
@Slf4j
public class HessianEncoder implements Encoder {
    @Override
    public byte[] encode(Object bean) {
        Hessian2Output ho = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            ho = new Hessian2Output(baos);
            ho.writeObject(bean);
            ho.flush();
            return baos.toByteArray();
        } catch (Exception ex) {
            throw new IllegalArgumentException("HessianUtils.serialize.异常.", ex);
        } finally {
            if (null != ho) {
                try {
                    ho.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
